package org.xmdl.silk.wrap;

import org.xmdl.silk.Expression;
import org.xmdl.silk.Variable;
import org.xmdl.silk.VariableRef;
import org.xmdl.silk.impl.VariableRefImpl;
import org.xmdl.silk.interpreter.UndefinedVariableException;
import org.xmdl.silk.visit.Visitor;

public class VariableRefWrap extends VariableRefImpl implements Expression, ExpressionWrap {

	private final VariableRef w;

	public VariableRefWrap(VariableRef w) {
		this.w = w;
		variable = WrapUtils.variable(w.getVariable());
	}

	@Override
	public Variable getVariable() {
		return variable;
	}

	@Override
	public void setVariable(Variable newVariable) {
		variable = WrapUtils.variable(newVariable);
		w.setVariable(newVariable);
	}

	public boolean accept(Visitor visitor)
	{
		if (visitor.visitEnter(this)) {
			Wrapped v = (Wrapped) getVariable();
			v.accept(visitor);
		}
		boolean result = visitor.visitLeave(this);
		if (!result)
			throw new UndefinedVariableException(variable);
		return result;
	}
}
